package com.ecp.commons.message;

import java.io.ByteArrayInputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.ecp.base.service.SystemArgumentService;
import com.ecp.util.HttpRequestUtil;
import com.ecp.util.SpringUtil;

/** * @author  wangyin: 
    * @date    2015年11月10日 下午5:51:40
    */
public class MWSUtil {
	private static final Logger				logger		= Logger.getLogger(MWSUtil.class);

	private final static String				HOST		= "http://ws.montnets.com";
	private final static String				HOST_YD		= "http://223.68.139.178:9010/YidaInterface/SendSms.do";
	private final static String				SENDPORT	= "7903";
	private final static String				USERNAME	= "J21266";
	private final static String				PASSWORD	= "565795";

	private static SystemArgumentService	systemArgumentService;

	public static Boolean sendMessage(String tel, String content) {
		boolean ok = false;
		HttpRequestUtil hrUtil = new HttpRequestUtil();
		String result = null;
		boolean flag = true;
		try {
			if (systemArgumentService == null) {
				systemArgumentService = (SystemArgumentService) SpringUtil
						.getBean("systemArgumentService");
			}
			String operator = systemArgumentService.getStringValueByArgKey("operator");
			if (StringUtils.isNotEmpty(operator)) {
				if (operator.equals("YidaTest")) {
					flag = false;
				}
			}
			if (flag) {
				StringBuilder uri = new StringBuilder();
				uri.append(HOST).append(":").append(SENDPORT);
				uri.append("/MWGate/wmgw.asmx/MongateCsSpSendSmsNew");
				uri.append("?userId=").append(USERNAME);
				uri.append("&password=").append(PASSWORD);
				uri.append("&pszMobis=").append(tel);
				try {
					uri.append("&pszMsg=").append(URLEncoder.encode(content, "UTF-8"));
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
				}
				uri.append("&iMobiCount=").append("1");
				uri.append("&pszSubPort=").append("*");
				result = hrUtil.doRequestByGet(uri.toString());
			} else {
				String postUrl = HOST_YD;
				String PostData = "sname=bjymt&spwd=111111&scorpid=2&sphones=" + tel + "&smsg="
						+ java.net.URLEncoder.encode(content, "utf-8");
				result = HttpRequestUtil.SMS(PostData, postUrl);
			}
			logger.info(tel + ";" + content + ";" + result);

			if (StringUtils.isNotEmpty(result)) {
				SAXReader saxReader = new SAXReader();
				Document document = null;
				try {
					if (flag) {
						document = saxReader
								.read(new ByteArrayInputStream(result.getBytes("UTF-8")));
						Element stringEl = document.getRootElement();
						String code = stringEl.getText();
						if (code.length() == 20) {
							ok = true;
						}
					} else {
						String[] codes = result.split(",");
						if (codes != null && codes.length > 0) {
							for (String cd : codes) {
								if (cd.contains("success")) {
									ok = true;
								}
							}
						}
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		return ok;
	}
}
